﻿using System.Windows.Forms;

namespace GeoSketch2.Core
{
    /// <summary> 
    /// The tool to draw lines
    /// </summary>
    public class TLine:BaseTool
    {
        #region Fields
        /// <summary> 
        /// The current polyline
        /// </summary>
        public Polyline CurrentPolyline;

        //True if the second point of the line has not been clicked
        bool _ClicRemaining;
        #endregion

        #region Constructor
        /// <summary> 
        /// The tool to draw Archaeolines
        /// </summary>
        /// <param name="Viewport">The viewport to draw to</param>
        public TLine(BaseViewport Viewport)
            : base(Viewport,"Line")
        {
            _ClicRemaining = false;
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            ExternalPointSent(Viewport.Object2ImageTransformation.InverseTransform(MouseArgs.Location));
        }

        /// <summary>
        /// The method called when a text is sent to the tool
        /// </summary>
        public override void TextEntry(string Text)
        {
            ExternalPointSent(GetCoordinatesFromString(Text));
        }

        /// <summary>
        /// The method called when a point in object coordinates is sent to the tool
        /// </summary>
        public override void ExternalPointSent(Point2D64D Point)
        {
            SendMessage("Click the first point, or type its coordinates");
            if (!_ClicRemaining)
            {
                NewPolyline();
                SendMessage("Click the second point, or type its coordinates");
            }

            _ClicRemaining = !_ClicRemaining;
            CurrentPolyline.Vertices.Add(new Vertex(Point, VertexType.Line));
            CurrentPolyline.Draw(Viewport);
        }
        #endregion

        #region Methods
        /// <summary>
        /// Creates a new polyline
        /// </summary>
        public void NewPolyline()
        {
            CurrentPolyline = new Polyline(null,Viewport.CurrentLayer, Viewport.CurrentLineType,Viewport.CurrentColor);
            Viewport.GeoObjects.Add(CurrentPolyline);
        }

        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Click the first point, or type its coordinates");
        }
        #endregion
    }
}

